<?php
/**
 * Created by PhpStorm.
 * User: lyq
 * Date: 2018/12/13
 * Time: 18:03
 */
namespace app\wechat\controller;

use app\common\model\ServerException;
use app\common\model\User;
use app\common\model\UserException;
use think\Db;
use app\common\model\Jhsms;
use app\common\model\SMS;
use think\Exception;
use think\Session;

class Utils extends Common
{
    public function down(){
        return $this->fetch();
    }

    //发送验证码
    public function sendCode()
    {
        if(request()->isAjax()){
            try{
                //是否已授权
                $wechat = session("WECHAT_USER_INFO");
                if(!$wechat||empty($wechat['unionid'])){
                    $this->ajaxError('未授权登录！');
                }
                //是否重复绑定
                $find = Db::name("user")
                    ->where('unionid',$wechat['unionid'])
                    ->find();
                if($find){
                    $this->ajaxError('您已绑定手机号码，请勿重复操作！');
                }
                //手机号
                $mobile = trim(input('mobile'));
                if(empty($mobile)){
                    $this->ajaxError('请输入手机号码！');
                }
                if(!preg_match(config('tel_regular'),$mobile)){
                    $this->ajaxError('手机号码有误！');
                }
                $find = Db::name("user")
                    ->where('mobile',$mobile)
                    ->find();
                if($find&&!empty($find['unionid'])){
                    $this->ajaxError('该手机号已绑定微信，请更换手机号码！');
                }
                //发送短信
                $Jhsms = new Jhsms();
                $Jhsms->send_sms($mobile,5);
                $this->ajaxSuccess('短信验证码已发送，请注意查收！');
            }catch (\Exception $exception){
                global $wechat_has_exception;
                $wechat_has_exception = true;
                $this->ajaxError($exception->getMessage());
            }
        }
    }

    //绑定手机号
    public function band()
    {
        if(request()->isAjax()){
            try{
                //是否已授权
                $wechat = session("WECHAT_USER_INFO");
                if(!$wechat||empty($wechat['unionid'])){
                    $this->ajaxError('未授权登录！');
                }
                //是否重复绑定
                $find = Db::name("user")
                    ->where('unionid',$wechat['unionid'])
                    ->find();
                if($find){
                    $this->ajaxError('您已绑定手机号码，请勿重复操作！');
                }
                //手机号
                $mobile = trim(input('mobile'));
                if(empty($mobile)){
                    $this->ajaxError('请输入手机号码！');
                }
                if(!preg_match(config('tel_regular'),$mobile)){
                    $this->ajaxError('手机号码有误！');
                }
                //验证码
                $code = trim(input('code'));
                //验证
                SMS::verifyCaptcha($mobile,$code,5);

                //查找用户
                $find = Db::name("user")
                    ->where('mobile',$mobile)
                    ->find();
                if($find){
                    //手机号已注册
                    if(!empty($find['unionid'])){
                        $this->ajaxError('该手机号已绑定微信，请更换手机号码！');
                    }
                    $band = Db::name('user')
                        ->where('id',$find['id'])
                        ->update(['unionid'=>$wechat['unionid'],'openid'=>$wechat['openid'],'update_time'=>now_datetime()]);
                    if(!$band){
                        throw new ServerException('服务器繁忙，请稍后再试！');
                    }
                    $session = $find;
                    $session['unionid'] = $wechat['unionid'];
                    $str = '手机绑定成功';
                }else{
                    //未注册
                    $nick_name = filterEmoji($wechat['nickname']);
                    $nick_name = empty($nick_name)?'已重置':$nick_name;
                    $password = substr($mobile,-6);
                    $parent_mobile = trim(input('post.parent_mobile'));
                    $parent_mobile = base64_decode($parent_mobile);
                    $uid = User::register($nick_name,$mobile,$password,$password,$parent_mobile,$wechat['unionid'],$wechat['openid'],$wechat['headimgurl']);
                    $str = '绑定成功，初始登录密码为手机号后六位，可以登录APP修改';
                    $session = [
                        'id'=>$uid
                    ];
                }
                //用户信息写入session
                Session::delete("WECHAT_USER_INFO");
                session("H_LOGIN_USER",$session);
                $this->ajaxSuccess($str);
            }catch (\Exception $exception){
                global $wechat_has_exception;
                $wechat_has_exception = true;
                $this->ajaxError($exception->getMessage());
            }
        }
    }
}